package com.secneo.appshare.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.UserInfo;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.ActivityUtil;
import com.secneo.mp.api.database.DatabaseHelper;

public class RegisterActivity extends Activity
{
	public EditText registerAccountEditText, registerPasswordEditText, registerRePasswordEditText, registerEmailEditText;
	public EditText recommenderAndroidIdEditText;
	public Button registerButton;
	public String registerAccountString, registerPasswordString, registerRePasswordString, registerEmailString;
	public String recommenderAndroidIdString;

	private ProgressDialog mNetProgressBar = null;
	
	private boolean register_published = false;
	
	public Handler mHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case 1:
					if (mNetProgressBar != null) 
					{
						mNetProgressBar.dismiss();
						mNetProgressBar = null; 
					}
					ActivityUtil.alert(RegisterActivity.this, "注册失败，用户名重复");
					break;
				case 2:
					if (mNetProgressBar != null) 
					{
						mNetProgressBar.dismiss();
						mNetProgressBar = null; 
					}
					ActivityUtil.alert(RegisterActivity.this, "注册失败，未知错误");
					break;
				case 3:
					if (mNetProgressBar != null) 
					{
						mNetProgressBar.dismiss();
						mNetProgressBar = null; 
					}
					ActivityUtil.alert(RegisterActivity.this, "网络异常，请检查网络连接");
					break;
				case 4:
					if (mNetProgressBar != null) 
					{
						LocalStore.getLocaleStoreInstance(RegisterActivity.this).setHonorCounts(1) ;
						mNetProgressBar.dismiss();
						mNetProgressBar = null; 
					}
					finish();
					break;
				default:
					if (mNetProgressBar != null) 
					{
						mNetProgressBar.dismiss();
						mNetProgressBar = null; 
					}
					break;

			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.appshare_register);
		AppShareApplication.getInstance().addActivity(this);
		registerAccountEditText = (EditText) findViewById(R.id.RegisterAccountEditText);
		registerPasswordEditText = (EditText) findViewById(R.id.RegisterPasswordEditText);
		registerRePasswordEditText = (EditText) findViewById(R.id.RegisterRePasswordEditText);
		registerEmailEditText = (EditText) findViewById(R.id.RegisterEmailEditText);
		recommenderAndroidIdEditText = (EditText) findViewById(R.id.RecommenderAndroidIdEditText);
		registerButton = (Button) findViewById(R.id.RegisterButton);

		register_published = false;

		registerButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (ActivityUtil.isEmpty(registerAccountEditText, registerPasswordEditText, registerRePasswordEditText))
				{
					ActivityUtil.alert(RegisterActivity.this, "必填信息不能为空");
					return;
				} 
				else
				{
					DatabaseHelper dh=new DatabaseHelper(RegisterActivity.this);
					dh.insertUserAppPopularityBycatagory(RegisterActivity.this,2046);
					dh.close();
					
					recommenderAndroidIdString = recommenderAndroidIdEditText.getText().toString().trim();
					registerAccountString = registerAccountEditText.getText().toString().trim();
					registerPasswordString = registerPasswordEditText.getText().toString().trim();
					registerRePasswordString = registerRePasswordEditText.getText().toString().trim();
					registerEmailString = registerEmailEditText.getText().toString().trim();
					
					if( 
							recommenderAndroidIdString.length()>0 
							&& ( recommenderAndroidIdString.length()<6 || recommenderAndroidIdString.length()>8 )
						)
					{
						ActivityUtil.alert(RegisterActivity.this, "推荐人安卓号应为6-8位的数字");
						return;
					}
					
					int registerName = ActivityUtil.chineseLength(registerAccountString);
					if (registerName < 3 || registerName > 14)
					{
						ActivityUtil.alert(RegisterActivity.this, "昵称不正确");
						return;
					}
					boolean passwordCommon = registerPasswordString.equals(registerRePasswordString);
					if (!passwordCommon)
					{
						ActivityUtil.alert(RegisterActivity.this, "密码输入不一致");
						return;
					}
					if (registerEmailString.length() != 0)
					{
						if (!ActivityUtil.isEmail(registerEmailString))
						{
							ActivityUtil.alert(RegisterActivity.this, "邮箱不输入不合法");
							return;
						}
					}
					mNetProgressBar = ProgressDialog.show(RegisterActivity.this, null,
							 "正在提交,请稍候..." ,true);
					Thread zhuceThread = new Thread()
					{
						public void run()
						{
							RemoteApiImpl rai = new RemoteApiImpl();
							UserInfo ret = rai.addNewUser(RegisterActivity.this, recommenderAndroidIdString, registerAccountString, "01", registerPasswordString, registerEmailString, "", "");
							if (ret != null)
							{
								if (!"".equals(ret.id) && ret.id != null)
								{
									LocalStore.getLocaleStoreInstance(RegisterActivity.this).setAndroidId(ret.androidId);
									LocalStore.getLocaleStoreInstance(RegisterActivity.this).setNickName(registerAccountString);
									LocalStore.getLocaleStoreInstance(RegisterActivity.this).setUid(ret.id);
									LocalStore.getLocaleStoreInstance(RegisterActivity.this).setAvatarName("buildin:01");
									LocalStore.getLocaleStoreInstance(RegisterActivity.this).setBangAccountActive();
									Intent intent = new Intent();
									intent.setClass(RegisterActivity.this, MemberSettingHeaderActivity.class);
									intent.putExtra("registerFlag", true);
									startActivity(intent);

									Message msg = new Message(); // 注册成功
									msg.what = 4;
									mHandler.sendMessage(msg);
								} 
								else
								{
									register_published = false;
									
									if ("dup".equals(ret.error))
									{
										Message msg = new Message();
										msg.what = 1;
										mHandler.sendMessage(msg);
										return;
									} 
									else
									{
										Message msg = new Message();
										msg.what = 2;
										mHandler.sendMessage(msg);
									}
								}
							} 
							else
							{
								register_published = false;
								
								Message msg = new Message();
								msg.what = 3;
								mHandler.sendMessage(msg);
							}
						}
					};
					if(false == register_published){
						zhuceThread.start();
						register_published = true;
					}
					else{
						Log.e("Register", "Captured one!!!");
					}
				}

			}
		});
	}
}
